package com.alex.seo.aspect;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

@Aspect
@Order(2)
public class PerformanceAspect extends GenericPointcuts {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(PerformanceAspect.class);
	
	private static final NumberFormat NUMBER_FORMAT = new DecimalFormat("#.###");

	@Around(value = "service() && !logging() || job()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		long time = System.currentTimeMillis();
		Object result = joinPoint.proceed();
		time = System.currentTimeMillis() - time;
		
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info(joinPoint.getSignature().toShortString() + " took " + NUMBER_FORMAT.format((float) time / 1000) + " seconds");
		}
		
		return result;
	}

}
